<template>
	<div>
		<AplidForm ref="formRef" v-bind="BaseFormConfig" v-model="formModel" @change="handleItemChange" @submit="onSubmit"> </AplidForm>
	</div>
</template>

<script setup lang="ts">
import { ElMessageBox } from 'element-plus'
import apiMod from '@/apiMod'
import { BaseFormConfig } from '../../config'
import { getAreaObj } from '@/utils/util'
import { useCommonStore } from '@/stores/common'
const { setTempParams } = useCommonStore()

const emit = defineEmits(['refresh', 'back'])
const formModel: any = ref({})
const formRef = ref()
const onSubmit = (form, done) => {
	const areaObj: any = getAreaObj(form.areas)
	const areaNameObj: any = getAreaObj(form.areas_name)
	const crAreasObj: any = getAreaObj(form.crAreas, 'cr')
	const crAreasNameObj: any = getAreaObj(form.cr_areas, 'cr')
	const params = {
		...form,
		...areaObj,
		...crAreasObj,
		...areaNameObj,
		...crAreasNameObj,
    seniorLiving:1,
    seniorLivingLabel:'社区居家养老',
    buildInfo:undefined,
		areas: undefined,
		crAreas: undefined,
		code: 1001,
	}
   if(form.buildInfo) {
    params.moveInBuilding =form.buildInfo.join(',')
  }


	setTempParams({
		idCard: params.idCard,
	})
  
	apiMod
		.view_dpServiceObject_gather(params)
		.then((res) => {
			emit('refresh', res.data)
			done()
		})
		.catch((err) => {
			done()
		})
}

const openTip = () => {
	ElMessageBox.confirm('数据已采集，身份证号重复?', '提示', {
		confirmButtonText: '退出',
		cancelButtonText: '重新输入',
		type: 'warning',
	})
		.then(() => {
			emit('back')
		})
		.catch(() => {
			formModel.value.idCard = undefined
		})
}

const handleItemChange = (model, item, e) => {
	// 校验身份证号是否已存在
	if (item.prop == 'idCard' && e) {
		apiMod.view_dpServiceObject_page({ idCard: e }).then((res) => {
			const data = res.data
			if (data.records.length > 0) {
				openTip()
			}
		})
	}
}
const onSave = () => {
	formRef.value!.onSubmit()
}
defineExpose({
	onSave,
})
</script>

<style scoped></style>
